package it.enrico.mangamania.inventario.client.panel;

import it.enrico.mangamania.inventario.client.MangaManiaInventario;
import it.enrico.mangamania.inventario.client.SalvaFumettoService;
import it.enrico.mangamania.inventario.client.SalvaFumettoServiceAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class CaricamentoFumettiPanel extends VerticalPanel {
	
	
	private final SalvaFumettoServiceAsync salvaFumettoService = GWT
	.create(SalvaFumettoService.class);

	final MangaManiaInventario mm;
	final FormPanel form = new FormPanel();
	final VerticalPanel holder = new VerticalPanel();
	final Button cancellaFumetti = new Button("Cancella fumetti");
	
	
	
	public CaricamentoFumettiPanel(MangaManiaInventario mangamania) {
		mm = mangamania;
		form.setEncoding(FormPanel.ENCODING_MULTIPART);
		form.setMethod(FormPanel.METHOD_POST);
		form.addStyleName("table-center");
		form.addStyleName("demo-panel-padded");
		form.setWidth("275px");
		FileUpload upload = new FileUpload();
		upload.setName("upload");
		form.setAction("/ciao");
		form.add(upload);
		holder.add(new Button("Submit", new ClickHandler() {
			
			public void onClick(ClickEvent event) {
				form.submit();
				
//				salvaFumettoService.retireveFumetti(new AsyncCallback<List<Fumetto>>() {
//
//					public void onFailure(Throwable caught) {
//						// TODO Auto-generated method stub
//
//					}
//
//					public void onSuccess(List<Fumetto> result) {
//						mm.setFumettiAll(result);
//						Window.alert("Fumetti caricati con successo");
//
//					}
//				});
				
			}
		}));
		
		cancellaFumetti.addClickHandler(new ClickHandler() {
			
			public void onClick(ClickEvent event) {
				salvaFumettoService.deleteFumetti(mm.getFumettiAll(), new AsyncCallback<String>() {
					
					public void onSuccess(String result) {
						Window.alert("Fumetti cancellati con successo");
						
					}
					
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						
					}
				});
				
			}
		});
		
		holder.add(cancellaFumetti);
		
		this.add(holder);
		this.add(form);
		RootPanel.get("carica").add(this);
	}

}
